Public Class OrderHistoryReportByTypeCtrl
    Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents litTBody As System.Web.UI.WebControls.Literal
    Protected WithEvents litTotal40Feet As System.Web.UI.WebControls.Literal
    Protected WithEvents litTotal20Feet As System.Web.UI.WebControls.Literal
    Protected WithEvents litTotal40HCFeet As System.Web.UI.WebControls.Literal
    Protected WithEvents litTotalInvoiceValue As System.Web.UI.WebControls.Literal

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private TR_START_TAG As String = "<TR>"
    Private TR_END_TAG As String = "</TR>"
    Private TD_START_TAG As String = "<TD"
    Private TD_CLASS As String = "class=bgrow1"
    Private TD_ALIGN_CENTER As String = "align=center"
    Private TD_ALIGN_RIGHT As String = "align=right"
    Private TD_WIDTH As String = "width=10%"
    Private TD_CLOSED_TAG As String = ">"
    Private TD_END_TAG As String = "</TD>"

    '0: 20', 1: 40', 2: 40' HC
    Private CONTAINER_20_FEET As Byte = 0
    Private CONTAINER_40_FEET As Byte = 1
    Private CONTAINER_40HC_FEET As Byte = 2    

#Region " Properties"
    Public Property DataReport() As DataTable
        Get
            Return CType(ViewState("DataReport"), DataTable)
        End Get
        Set(ByVal Value As DataTable)
            ViewState("DataReport") = Value
        End Set
    End Property

    Public Property Total20Feet() As Integer
        Get
            If Not ViewState("Total20Feet") Is Nothing Then
                Return CInt(ViewState("Total20Feet"))
            Else
                Return 0
            End If
        End Get
        Set(ByVal Value As Integer)
            ViewState("Total20Feet") = Value
        End Set
    End Property

    Public Property Total40Feet() As Integer
        Get
            If Not ViewState("Total40Feet") Is Nothing Then
                Return CInt(ViewState("Total40Feet"))
            Else
                Return 0
            End If
        End Get
        Set(ByVal Value As Integer)
            ViewState("Total40Feet") = Value
        End Set
    End Property

    Public Property Total40HCFeet() As Integer
        Get
            If Not ViewState("Total40HCFeet") Is Nothing Then
                Return CInt(ViewState("Total40HCFeet"))
            Else
                Return 0
            End If
        End Get
        Set(ByVal Value As Integer)
            ViewState("Total40HCFeet") = Value
        End Set
    End Property

    Public Property TotalInvoiceValue() As Double
        Get
            If Not ViewState("TotalInvoiceValue") Is Nothing Then
                Return CDbl(ViewState("TotalInvoiceValue"))
            Else
                Return 0
            End If
        End Get
        Set(ByVal Value As Double)
            ViewState("TotalInvoiceValue") = Value
        End Set
    End Property

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here        
    End Sub

    Public Sub GenerateReport()
        Me.Total20Feet = 0
        Me.Total40Feet = 0
        Me.Total40HCFeet = 0
        Me.TotalInvoiceValue = 0

        Me.litTBody.Text = Me.GetTBodyHTML()
        Me.litTotal40Feet.Text = Me.Total40Feet.ToString()
        Me.litTotal20Feet.Text = Me.Total20Feet.ToString()
        Me.litTotal40HCFeet.Text = Me.Total40HCFeet.ToString()
        Me.litTotalInvoiceValue.Text = FormatNumber(Me.TotalInvoiceValue, 2)
    End Sub
    Public Function GetTBodyHTML() As String
        Dim str As New System.Text.StringBuilder

        Dim row As DataRow
        For Each row In DataReport.Rows
            str.Append(Me.TR_START_TAG)

            'str.Append(Me.GetDateColumnHTML(CType(row.Item(0), Date).ToShortDateString))
            str.Append(Me.GetDateColumnHTML(FormatDate(CType(row.Item(0), Date))))
            str.Append(Me.GetCustomerNoColumnHTML(CStr(row.Item(1))))
            Dim containerType As Byte = CByte(row.Item(2))
            Dim containerValue As Integer = CInt(row.Item(3))

            If (containerType = Me.CONTAINER_40_FEET) Then
                str.Append(Me.GetContainerColumnHTML(CStr(containerValue)))
                str.Append(Me.GetContainerColumnHTML(" "))
                str.Append(Me.GetContainerColumnHTML(" "))
                Me.Total40Feet = Me.Total40Feet + containerValue
            Else
                If containerType = Me.CONTAINER_20_FEET Then
                    str.Append(Me.GetContainerColumnHTML(" "))
                    str.Append(Me.GetContainerColumnHTML(CStr(containerValue)))
                    str.Append(Me.GetContainerColumnHTML(" "))
                    Me.Total20Feet = Me.Total20Feet + containerValue
                Else ' 40HC
                    str.Append(Me.GetContainerColumnHTML(" "))
                    str.Append(Me.GetContainerColumnHTML(" "))
                    str.Append(Me.GetContainerColumnHTML(CStr(containerValue)))
                    Me.Total40HCFeet = Me.Total40HCFeet + containerValue
                End If
            End If

            Dim invoiceValue As Double = 0
            Try
                invoiceValue = CDbl(row.Item(4))
            Catch ex As Exception
            End Try
            str.Append(Me.GetInvoiceValueColumnHTML(FormatNumber(invoiceValue, 2)))
            str.Append(Me.TR_END_TAG)
            Me.TotalInvoiceValue = Me.TotalInvoiceValue + invoiceValue
        Next

        Return str.ToString()
    End Function

    Private Function GetDateColumnHTML(ByVal data As String) As String
        Dim Str As New System.Text.StringBuilder

        Str.Append(Me.TD_START_TAG + " ")
        Str.Append(Me.TD_CLASS + " ")
        Str.Append(Me.TD_ALIGN_CENTER)
        Str.Append(Me.TD_CLOSED_TAG)
        Str.Append(data)
        Str.Append(Me.TD_END_TAG)

        Return Str.ToString()
    End Function

    Private Function GetCustomerNoColumnHTML(ByVal data As String) As String
        Dim Str As New System.Text.StringBuilder

        Str.Append(Me.TD_START_TAG + " ")
        Str.Append(Me.TD_CLASS + " ")
        Str.Append(Me.TD_ALIGN_CENTER)
        Str.Append(Me.TD_CLOSED_TAG)
        Str.Append(data)
        Str.Append(Me.TD_END_TAG)

        Return Str.ToString()
    End Function

    Private Function GetContainerColumnHTML(ByVal data As String) As String
        Dim Str As New System.Text.StringBuilder

        Str.Append(Me.TD_START_TAG + " ")
        Str.Append(Me.TD_CLASS + " ")
        Str.Append(Me.TD_ALIGN_RIGHT + " ")
        Str.Append(Me.TD_WIDTH)
        Str.Append(Me.TD_CLOSED_TAG)
        Str.Append(data)
        Str.Append(Me.TD_END_TAG)

        Return Str.ToString()
    End Function

    Private Function GetInvoiceValueColumnHTML(ByVal data As String) As String
        Dim Str As New System.Text.StringBuilder

        Str.Append(Me.TD_START_TAG + " ")
        Str.Append(Me.TD_CLASS + " ")
        Str.Append(Me.TD_ALIGN_RIGHT)
        Str.Append(Me.TD_CLOSED_TAG)
        Str.Append(data)
        Str.Append(Me.TD_END_TAG)

        Return Str.ToString()
    End Function

End Class
